package com.zzf.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzf.commonutils.R;
import com.zzf.eduservice.client.OrderClient;
import com.zzf.eduservice.entity.EduCourse;
import com.zzf.eduservice.entity.EduTeacher;
import com.zzf.eduservice.entity.vo.CourseInfoVo;
import com.zzf.eduservice.entity.vo.CoursePublishVo;
import com.zzf.eduservice.entity.vo.CourseQuery;
import com.zzf.eduservice.entity.vo.TeacherQuery;
import com.zzf.eduservice.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-04-05
 */
@Api(description = "课程管理")
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    //添加课程基本信息
    @ApiOperation("添加课程基本信息")
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){

        String id = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    //根据课程id查询课程基本信息
    @ApiOperation("根据课程id查询课程基本信息")
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){

        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);


        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    //修改课程信息
    @ApiOperation("修改课程信息")
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据课程ID查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo coursePublishInfoVo = courseService.publishCourseInfo(id);
        return R.ok().data("courseInfo", coursePublishInfoVo);
    }

    //发布课程
    @PostMapping("publishCourse/{id}")
    public R publishCoursh(@PathVariable String id) {

        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        boolean update = courseService.updateById(eduCourse);
        if (update) {
            return R.ok();
        } else {
            return R.error().message("发布课程失败");
        }
    }

    //课程列表
    //TODO完善条件查询,分页功能
    @GetMapping("getCourseList/{current}/{limit}")
    public R getCourseList(@PathVariable long current,@PathVariable long limit) {
        Page<EduCourse> coursePage = new Page<>(current,limit);
        courseService.page(coursePage,null);

        //数据总条数
        long total = coursePage.getTotal();
        List<EduCourse> records = coursePage.getRecords();

        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",records);

        List<EduCourse> list = courseService.list(null);
        return R.ok().data(map);
    }

    //4:条件查询带分页
    @ApiOperation(value = "条件查询课程")
    @PostMapping("getCourseList/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) CourseQuery courseQuery){
        //创建page对象
        Page<EduCourse> coursePage = new Page<>(current,limit);
        //构建条件
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //多条件组合查询
        //mybatis学过动态sql;现在使用mybatis plus来做
        String name = courseQuery.getName();
        Integer status = courseQuery.getStatus();
        String begin = courseQuery.getBegin();
        String end = courseQuery.getEnd();
        //判断条件是否为空,如果不为空拼接条件
        if (!StringUtils.isEmpty(name)){
            //构建条件
            wrapper.like("title",name);
        }
        if (!StringUtils.isEmpty(status)){
            String statusStr;
            if (status == 0){
                statusStr = "Draft";
            }else {
                statusStr = "Normal";
            }
            wrapper.eq("status",statusStr);
        }
        if (!StringUtils.isEmpty(begin)){
            //大于等于开始时间
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            //小于等于结束时间
            wrapper.le("gmt_create",end);
        }

        //排序
        wrapper.orderByDesc("gmt_create");

        //把分页所有数据封装到pageTeacher对象里面
        courseService.page(coursePage, wrapper);

        //数据总条数
        long total = coursePage.getTotal();
        List<EduCourse> records = coursePage.getRecords();

        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",records);

        return R.ok().data(map);
    }

    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){

        courseService.removeCourse(courseId);

        return R.ok();
    }
}

